package dhm.com.dhmshop.view.mine.myshop;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.zhy.http.okhttp.OkHttpUtils;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dhm.com.dhmshop.R;
import dhm.com.dhmshop.adapter.MainVpFgAdapter;
import dhm.com.dhmshop.base.BaseActiity;
import dhm.com.dhmshop.base.Presenter.PressenterImpl;
import dhm.com.dhmshop.base.netWork.Constant;
import dhm.com.dhmshop.base.netWork.LoginContract;
import dhm.com.dhmshop.entity.Bean;
import dhm.com.dhmshop.entity.GetCollectionShop;
import dhm.com.dhmshop.entity.GetMyshop;
import dhm.com.dhmshop.entity.HeaderIMage;
import dhm.com.dhmshop.fragment.wode.shop.ShopFregment;
import dhm.com.dhmshop.utils.BitmapUtil;
import dhm.com.dhmshop.utils.HeaderImageCallback;
import dhm.com.dhmshop.utils.SpUtils;
import dhm.com.dhmshop.utils.TakePhotoUtil;
import me.nereo.multi_image_selector.MultiImageSelector;
import okhttp3.Call;

public class ShopdetailActivity extends BaseActiity implements LoginContract.IView {
    @BindView(R.id.image)
    SimpleDraweeView image;
    @BindView(R.id.main_tablayout)
    TabLayout mainTablayout;
    @BindView(R.id.main_vp)
    ViewPager mainVp;
    @BindView(R.id.edit)
    CheckBox edit;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.del)
    CheckBox del;
    @BindView(R.id.collection)
    ImageView collection;
    @BindView(R.id.shoper)
    LinearLayout shoper;
    private ArrayList<String> titles;
    private ArrayList<Fragment> fragments;
    private MainVpFgAdapter mainVpFgAdapter;
    private ShopFregment shopFregment;
    private ShopFregment shopFregment1;
    private ShopFregment shopFregment2;

    private PressenterImpl pressenter;
    private String uid;
    private String pid;
    private boolean collection1 = false;
    private String huid;
    private String type;
    private GetMyshop myshop;


    @Override
    protected int getLayout() {
        return R.layout.activity_shopdetail;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        Intent intent = getIntent();
        pid = intent.getStringExtra("pid");
//        type:3.用户   2.商家
        type = intent.getStringExtra("type");
        huid = intent.getStringExtra("huid");
        uid = SpUtils.getString(this, "uid");
        pressenter = new PressenterImpl();
        pressenter.attachView(this);
        if (type.equals("2")) {
            shoper.setVisibility(View.VISIBLE);
            collection.setVisibility(View.GONE);
        } else {
            shoper.setVisibility(View.GONE);
            collection.setVisibility(View.VISIBLE);
        }

        mainTablayout.setSelectedTabIndicatorHeight(0);
        //创建tab
        titles = new ArrayList<>();
        titles.add("推荐");
        titles.add("全部宝贝");
        titles.add("新品");

        Map<String, String> map = new HashMap<>();
        map.put("token", Constant.TOKEN);
        map.put("uid", uid);
        pressenter.sendMessage(this, Constant.MyShopCollects, map, GetCollectionShop.class);

    }

    private int position = 0;

    @Override
    protected void initData() {
        mainTablayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                position = tab.getPosition();
                del.setChecked(false);
                edit.setChecked(false);
//                del.setChecked(((ShopFregment) fragments.get(position)).isIsdel());
//                edit.setChecked(((ShopFregment) fragments.get(position)).isIsedit());
//                ((ShopFregment) fragments.get(position)).setdelcheck(del.isChecked());
//                ((ShopFregment) fragments.get(position)).seteditcheck(edit.isChecked());
            }


            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        edit.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (del.isChecked()) {
                    Toast.makeText(ShopdetailActivity.this, "请完成删除功能", Toast.LENGTH_SHORT).show();
                    edit.setChecked(false);
                    return;
                }
                if (edit.isChecked()) {
                    edit.setText("完成");
                } else {
                    edit.setText("编辑");
                }
//                ((ShopFregment) fragments.get(position)).seteditcheck(edit.isChecked());
            }
        });
        del.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (edit.isChecked()) {
                    Toast.makeText(ShopdetailActivity.this, "请完成编辑功能", Toast.LENGTH_SHORT).show();
                    del.setChecked(false);
                    return;
                }
                if (del.isChecked()) {
                    del.setText("完成");
                } else {
                    del.setText("删除");
                }
//                ((ShopFregment) fragments.get(position)).setdelcheck(del.isChecked());
            }
        });

    }

    @OnClick({R.id.collection, R.id.back, R.id.image})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.collection:
                if (!isCollection) {
                    Map<String, String> map = new HashMap<>();
                    map.put("token", Constant.TOKEN);
                    map.put("uid", uid);
                    map.put("shop_id", pid);
                    pressenter.sendMessage(ShopdetailActivity.this, Constant.AddShopCollect, map, Bean.class);
                } else {
                    collection1 = true;
                    Map<String, String> map = new HashMap<>();
                    map.put("token", Constant.TOKEN);
                    map.put("uid", uid);
                    pressenter.sendMessage(this, Constant.MyShopCollects, map, GetCollectionShop.class);
                }

                break;
            case R.id.back:
                finish();
                break;
            case R.id.image:
                if (type.equals("2")) {
                    pickImage();
                }
                break;
            default:
        }
    }

    protected static final int REQUEST_STORAGE_READ_ACCESS_PERMISSION = 101;

    private void pickImage() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN // Permission was added in API Level 16
                && ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            requestPermission(Manifest.permission.READ_EXTERNAL_STORAGE,
                    getString(R.string.mis_permission_rationale),
                    REQUEST_STORAGE_READ_ACCESS_PERMISSION);
        }else {
            MultiImageSelector selector = MultiImageSelector.create(ShopdetailActivity.this);
            selector.showCamera(true);
            selector.count(1);
            selector.single();
            selector.origin(mSelectPath);
            selector.start(ShopdetailActivity.this, REQUEST_IMAGE);
        }
    }

    private void requestPermission(final String permission, String rationale, final int requestCode){
        if(ActivityCompat.shouldShowRequestPermissionRationale(this, permission)){
            new AlertDialog.Builder(this)
                    .setTitle(R.string.mis_permission_dialog_title)
                    .setMessage(rationale)
                    .setPositiveButton(R.string.mis_permission_dialog_ok, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            ActivityCompat.requestPermissions(ShopdetailActivity.this, new String[]{permission}, requestCode);
                        }
                    })
                    .setNegativeButton(R.string.mis_permission_dialog_cancel, null)
                    .create().show();
        }else{
            ActivityCompat.requestPermissions(this, new String[]{permission}, requestCode);
        }
    }
    private String urlone="";
    private static final int REQUEST_IMAGE = 2;
    private ArrayList<String> mSelectPath;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == REQUEST_IMAGE){
            if(resultCode == RESULT_OK){

                Bitmap bitmap = null;

                mSelectPath = data.getStringArrayListExtra(MultiImageSelector.EXTRA_RESULT);
                StringBuilder sb = new StringBuilder();
                for(String p: mSelectPath){
                    sb.append(p);
                    sb.append("\n");
                    bitmap = BitmapUtil.getScaleBitmap(p, 0, 0);
                }

                if (!TextUtils.isEmpty(TakePhotoUtil.saveTodisk(bitmap, Constant.HeadPath))) {
                    //图片的URl
                    urlone = TakePhotoUtil.saveTodisk(bitmap, Constant.HeadPath);
                    image.setImageBitmap(bitmap);
                    uploadImage(urlone);
                }else {
                    Toast.makeText(this, "选择出错", Toast.LENGTH_SHORT).show();
                }



            }
        }
    }

    private void uploadImage(final String url) {
        OkHttpUtils.post().url(Constant.PATH + Constant.ModifyLicense)
                .addFile("image", url, new File(url))
                .addParams("uid", uid)
                .addParams("shop_id", pid)
                .addParams("token", Constant.TOKEN)
                .build().execute(new HeaderImageCallback() {

            @Override
            public void onError(Call call, Exception e, int id) {
            }

            @Override
            public void onResponse(HeaderIMage response, int id) {
                Toast.makeText(ShopdetailActivity.this, "更新成功", Toast.LENGTH_SHORT).show();
                finish();
            }
        });

    }



    //有几个底部的item就写几个
    private void setupTabIcons() {
        //tablayout图文效果
        for (int i = 0; i < fragments.size(); i++) {
            mainTablayout.getTabAt(i).setCustomView(getView(i));
        }
    }

    @SuppressLint("NewApi")
    private View getView(int position) {
        View tabitem = LayoutInflater.from(this).inflate(R.layout.tabshop, null);
        TextView b = tabitem.findViewById(R.id.b);
        TextView tv = tabitem.findViewById(R.id.tabtv);
        tv.setText(titles.get(position));
        b.setBackgroundResource(R.drawable.tab_white);
        return tabitem;


    }

    private void changeTextColor(TabLayout tabLayout) {
        try {
            //拿到c
            Field field = TabLayout.class.getDeclaredField("mTabStrip");
            field.setAccessible(true);
            //拿mTabStrip属性里面的值
            Object mTabStrip = field.get(tabLayout);
            //通过mTabStrip对象来获取class类，不能用field来获取class类，参数不能写Integer.class
            Method method = mTabStrip.getClass().getDeclaredMethod("setSelectedIndicatorColor", int.class);
            method.setAccessible(true);
            method.invoke(mTabStrip, Color.parseColor("#ffffff"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private boolean isCollection = false;

    @Override
    public void requesta(Object data) {
        if (data instanceof GetCollectionShop) {
            GetCollectionShop getCollectionShop = (GetCollectionShop) data;
            if (getCollectionShop.getCode() == 1) {
                for (int i = 0; i < getCollectionShop.getData().size(); i++) {
                    if (pid.equals(getCollectionShop.getData().get(i).getShopId() + "")) {
                        collection.setImageResource(R.mipmap.commodditydetails_collected);
                        isCollection = true;
                        if (collection1) {
                            Map<String, String> map = new HashMap<>();
                            map.put("token", Constant.TOKEN);
                            map.put("uid", uid);
                            map.put("shop_id", pid);
                            map.put("cid", getCollectionShop.getData().get(i).getFavoriteId() + "");
                            pressenter.sendMessage(ShopdetailActivity.this, Constant.DelShopCollect, map, Bean.class);
                        }
                        i=getCollectionShop.getData().size();
                    } else {
                        isCollection = false;
                        collection.setImageResource(R.mipmap.commodditydetails_notcollected);
                    }
                }

            }
            Map<String, String> map1 = new HashMap<>();
            map1.put("token", Constant.TOKEN);
            map1.put("uid", huid);
            pressenter.sendMessage(this, Constant.GetMyShopinfo, map1, GetMyshop.class);


        } else if (data instanceof Bean) {
            Bean bean = (Bean) data;
            Toast.makeText(this, bean.getMessage(), Toast.LENGTH_SHORT).show();
            if (bean.getCode() == 1) {
                if (bean.getMessage().equals("收藏成功")) {
                    isCollection=true;
                    collection.setImageResource(R.mipmap.commodditydetails_collected);
                } else {
                    isCollection=false;
                    collection.setImageResource(R.mipmap.commodditydetails_notcollected);
                    collection1 = false;
                }
            }
        } else if (data instanceof GetMyshop) {
            myshop = (GetMyshop) data;
            title.setText(myshop.getData().getStore_name());
            image.setImageURI(Constant.PATH+"/"+ myshop.getData().getImg());
            //三个fragments
            fragments = new ArrayList<>();
//            shopFregment = new ShopFregment("1");
//            shopFregment1 = new ShopFregment("2");
//            shopFregment2 = new ShopFregment("3");
            fragments.add(shopFregment);
            fragments.add(shopFregment1);
            fragments.add(shopFregment2);
            //设置适配器
            mainVpFgAdapter = new MainVpFgAdapter(getSupportFragmentManager(), fragments, titles);
            changeTextColor(mainTablayout);
            //绑定适配器
            mainVp.setAdapter(mainVpFgAdapter);
            mainVp.setOffscreenPageLimit(0);
            //设置联动
            mainTablayout.setupWithViewPager(mainVp);

            setupTabIcons();//设置底部TabLayout的item

        }
    }

    @Override
    public void fail(String error) {

    }
}
